package com.bao.system.controller;


import com.bao.handler.BusinessException;
import com.bao.response.Result;
import com.bao.response.ResultCode;
import com.bao.system.entity.BUser;
import com.bao.system.service.UserService;
import com.bao.vo.UserVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author bao
 * @since 2021-06-03
 */
@RestController
@RequestMapping("/system/user")
@Api(value = "用户管理")
//@CrossOrigin
public class UserController {

    @Resource
    private UserService userService;

    @PostMapping("/findUserPage")
    @ApiOperation(value = "查询所有用户信息",notes = "查询所有用户")
    public Result findUserPage(@RequestParam(required = true, defaultValue = "1") Integer currentPage,
                               @RequestParam(required = true, defaultValue = "7") Integer pageSize,
                               @RequestBody UserVO userVO){
        System.out.println("------------------------");
        System.out.println(userVO);
        System.out.println(currentPage +"-"+ pageSize);
        Page<BUser> page = new Page<>(currentPage, pageSize);
        QueryWrapper<BUser> wrapper = getWrapper(userVO);
        IPage<BUser> userPage = userService.findUserPage(page, wrapper);
        long total = userPage.getTotal();
        List<BUser> BUserList = userPage.getRecords();
        System.out.println(BUserList);
        return Result.ok().data("total",total).data("userList", BUserList);
    }

    private QueryWrapper<BUser> getWrapper(UserVO userVO){
        QueryWrapper<BUser> wrapper = new QueryWrapper<>();
        if (userVO != null) {
            if(!StringUtils.isEmpty(userVO.getDepartmentId())){
                wrapper.eq("department_id",userVO.getDepartmentId());
            }
            if(!StringUtils.isEmpty(userVO.getUsername())){
                wrapper.eq("username",userVO.getUsername());
            }
            if(!StringUtils.isEmpty(userVO.getEmail())){
                wrapper.eq("email",userVO.getEmail());
            }
            if(!StringUtils.isEmpty(userVO.getSex())){
                wrapper.eq("sex",userVO.getSex());
            }
            if(!StringUtils.isEmpty(userVO.getNickname())){
                wrapper.eq("nickname",userVO.getNickname());
            }
        }
        return wrapper;
    }


    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个用户",notes = "通过id查询用户信息")
    public Result getUserById(@PathVariable("id") Long id){
        BUser BUser = userService.getUserById(id);
        System.out.println(id);
        System.out.println(BUser);
        if(BUser != null){
            return Result.ok().data("user", BUser);
        }else {
            throw new BusinessException(ResultCode.USER_ACCOUNT_NOT_EXIST.getCode(),ResultCode.USER_ACCOUNT_NOT_EXIST.getMsg());
        }
    }
    @GetMapping("/editStatus")
    @ApiOperation(value = "修改用户状态",notes = "通过id修改用户状态")
    public Result editStatus(@RequestParam("id") Long id, @RequestParam("status") Integer status){
        userService.editUser(id, status);
        return Result.ok();
    }
    @GetMapping("/editUser")
    @ApiOperation(value = "修改用户",notes = "通过id修改用户")
    public Result editUser(@RequestParam("id") Long id, @RequestParam("departmentId") Long departmentId){
        userService.editUserDep(id, departmentId);
        System.out.println(id+"--"+departmentId);
        return Result.ok();
    }
    @GetMapping("/deleUser")
    @ApiOperation(value = "删除用户",notes = "通过id删除用户")
    public Result deleUser(@RequestParam("id") Long id){
        System.out.println(id);
        userService.deleUser(id);
        return Result.ok();
    }

//    @PostMapping("/login")
//    @ApiOperation(value = "查询所有用户信息",notes = "查询所有用户")
//    public Result login(@RequestParam String username, @RequestParam String password){
//        System.out.println("------------------------");
//        System.out.println(username +"-"+ password);
//
//        BUser BUser = userService.login(username,password);
//        if(BUser == null)
//            return Result.error().msg(ResultCode.USER_ACCOUNT_NOT_EXIST.getMsg()).code(ResultCode.USER_ACCOUNT_NOT_EXIST.getCode());
//        else if (BUser.getStatus() == 0)
//            return Result.error().msg(ResultCode.USER_ACCOUNT_LOCKED.getMsg()).code(ResultCode.USER_ACCOUNT_LOCKED.getCode());
//        else
//            return  Result.ok().data("user", BUser);
//    }
}

